package dk.hewison.client.pages;

import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import dk.hewison.client.Log;
import dk.hewison.client.mvc.AbstractView;
import dk.hewison.client.mvc.View;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-07 20:27:07 +0000 (Sat, 07 Feb 2009) $:
 * @version $Revision: 362 $:
 */
public class PageStackView extends AbstractView<Integer> {


    private int currentPageId = -1;
    private DeckPanel deckPanel;


    public PageStackView() {
        Log.debug("PageStackView.PageStackView");
    }

    public Integer getModel() {
        return null;
    }

    public void setModel(Integer model) {
        Log.debug("PageStackView.setModel");
        showPage(model);
    }

    public void initView() {
        Log.debug("PageStackView.initView");
        deckPanel = new DeckPanel();
        deckPanel.add(new Label("PageStackView"));
        deckPanel.showWidget(0);
        deckPanel.setSize("100%", "100%");
        deckPanel.setStyleName("pagestack");
    }

    public void showPage(int pageId) {
        Log.debug("PageStackView.showPage");
        if (currentPageId != -1) {
            controller.getSubController(currentPageId).leavePage();
        }

        View view = controller.getSubView(pageId);
        int index = deckPanel.getWidgetIndex(view.getMyWidget());
        Log.debug("PageStackView.showPage pageId=" + pageId + " index=" + index);
        if (index == -1) {
            deckPanel.add(view.getMyWidget());
            showPage(pageId);
            return;
        }
        deckPanel.showWidget(index);
        currentPageId = pageId;
    }

    public Widget getMyWidget() {
        return deckPanel;
    }


    public void doLayout() {
        /*Iterator<Widget> it = view.getwidget().iterator();
        while (it.hasNext()) {
            Page page = (Page) it.next();
            page.doLayout();
        }*/
    }
}
